package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Videos;
import com.ruoyi.system.service.IVideosService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-04-08
 */
@RestController
@RequestMapping("/system/videos")
public class VideosController extends BaseController
{
    @Autowired
    private IVideosService videosService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:videos:list')")
    @GetMapping("/list")
    public TableDataInfo list(Videos videos)
    {
        startPage();
        List<Videos> list = videosService.selectVideosList(videos);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:videos:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Videos videos)
    {
        List<Videos> list = videosService.selectVideosList(videos);
        ExcelUtil<Videos> util = new ExcelUtil<Videos>(Videos.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:videos:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(videosService.selectVideosById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:videos:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Videos videos)
    {
        System.out.println("videos数据");
        System.out.println(videos);
        System.out.println("................");

        String videourls=videos.getVideourl();
        if(!StringUtils.isEmpty(videourls)){
            if(videourls.indexOf(",")!=-1){
                String[] strArray = videourls.split(",");
                for (int i = 0; i < strArray.length ; i++) {
                    Videos video = new Videos();
                    video.setCourseid(videos.getCourseid());
                    video.setVideourl(strArray[i]);
                    videosService.insertVideos(video);
                }
            }else{
                Videos video = new Videos();
                video.setCourseid(videos.getCourseid());
                video.setVideourl(videourls);
                videosService.insertVideos(video);
            }
            return toAjax(1);
        }
        return toAjax(0);
    }


    @PreAuthorize("@ss.hasPermi('system:videos:list')")
    @GetMapping("/getCourseVideosList/{courseId}")
    public TableDataInfo getCourseVideosList(@PathVariable Long courseId)
    {
        startPage();
        List<Videos> list = videosService.selectCourseVideosList(courseId);
        return getDataTable(list);
    }


    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:videos:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Videos videos)
    {

        System.out.println("videos数据");
        System.out.println(videos);
        System.out.println("................");

        String videoids=videos.getId();
        String videourls=videos.getVideourl();
        if(!StringUtils.isEmpty(videourls)){
            if(videourls.indexOf(",")!=-1){
                String[] idsArray=videoids.split(",");
                String[] strArray = videourls.split(",");
                for (int i = 0; i < strArray.length ; i++) {
                    Videos video = new Videos();
                    video.setId(idsArray[i]);
                    video.setCourseid(videos.getCourseid());
                    video.setVideourl(strArray[i]);
                    videosService.updateVideos(video);
                }
            }else{
                Videos video = new Videos();
                video.setCourseid(videos.getCourseid());
                video.setId(videoids);
                video.setVideourl(videourls);
                videosService.updateVideos(video);
            }
            return toAjax(1);
        }
        return toAjax(0);
        //return toAjax(videosService.updateVideos(videos));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:videos:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(videosService.deleteVideosByIds(ids));
    }
}
